<?php

class MyMultiCurl extends MultiCurl {

    public $siteUrl;
    public $cat_name;
    public $reg_exp_name;
    public $reg_exp_art;
    public $reg_exp_price;
    public $reg_exp_desc;
    public $reg_exp_har;
    public $reg_exp_img;
    public $search;
    public $replace;
    public $check;
    public $name;
    public $art;
    public $price;
    public $desc;
    public $har;
    public $imgs;
    public $id;

    public function __construct($siteUrl, $cat_name, $reg_exp_name, $reg_exp_art, $reg_exp_price, $reg_exp_desc, $reg_exp_har, $check, $reg_exp_img, $search, $replace) {
        parent::__construct();
        $this->siteUrl = $siteUrl;
        $this->cat_name = $cat_name;
        $this->reg_exp_name = $reg_exp_name;
        $this->reg_exp_art = $reg_exp_art;
        $this->reg_exp_price = $reg_exp_price;
        $this->reg_exp_desc = $reg_exp_desc;
        $this->reg_exp_har = $reg_exp_har;
        $this->search = $search;
        $this->replace = $replace;
        $this->check = $check;
        $this->reg_exp_img = $reg_exp_img;
        $this->reg_exp_charset = '#\<meta[^<>]+charset\s*\=\s*([\w\-]+)#';
        $this->id = 0;
    }

    private function clear($str) {
        $search = array("\r", "\n", ";");
        $replace = array(" ", "<br>", " ");
        $patterns = array('#<table.*>#Uis', '#<tr.*>#Uis', '#<td.*>#Uis', '#<ul.*>#Uis', '#<li.*>#Uis', '#<p.*>#Uis');
        $replacements = array("<table>", "<tr>", "<td>", "<ul>", "<li>", "<p>");
        $str = trim(strip_tags(preg_replace($patterns, $replacements, $str), "<table></table><tbody></tbody><tr></tr><td></td><p></p><b></b><ul></ul><li></li><strong></strong><em></em><i></i><br>"));
        $str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
        $str = str_replace($search, $replace, $str);
        return $str;
    }

    protected function onLoad($url, $response, $info) {
        if (preg_match($this->reg_exp_charset, $response, $m)) {
            if ($m[1] != "utf-8") {
                $response = iconv($m[1], 'utf-8', $response);
            }
        }
        if (preg_match($this->reg_exp_name, $response, $m)) {
            $this->name = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_art, $response, $m)) {
            $this->art = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_price, $response, $m)) {
            $this->price = str_replace(";", "", strip_tags($m[1]));
        }
        if (preg_match($this->reg_exp_desc, $response, $m)) {
            $this->desc = $this->clear($m[1]);
        }
        if (preg_match($this->reg_exp_har, $response, $m)) {
            if ($this->check == "off") {
                $this->har = $this->clear($m[1]);
            } else {
                $this->har = $this->clear($m[0]);
            }
        }
        //echo $this->reg_exp_img; die();
        if (preg_match_all($this->reg_exp_img, $response, $m)) {
            foreach ($m[1] as $id => $img) {
                $this->imgs[$id] = $img;
            }
        }

        $res = new Result($this);
        $res->add();
        $this->id++;
    }

}

?>
